/**
 * @author Hari K J
 */
package com.oolatina.fragments;

import java.util.HashMap;

import com.androidquery.AQuery;
import com.mayashree.manager.StateHolder;
import com.oolatina.activity.MainActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.ViewGroup;

/**
 * @author Hari K J
 * 
 */
public class BaseFragment extends Fragment implements OnClickListener {

	protected AQuery aq;
	
	protected String LOG_TAG;
	protected Context fragmentContext;
	protected StateHolder stateHolder;
	protected View mView;
	private View moreDataLayout;
	
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Fragment#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LOG_TAG += this.getClass().getSimpleName().toString() + " : ";
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Fragment#onCreateView(android.view.LayoutInflater,
	 * android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Fragment#onActivityCreated(android.os.Bundle)
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		aq = new AQuery(getActivity());
		
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
	}

	/**
	 * Had to be overridden by the Fragments who wants to save their states
	 */
	public StateHolder onSaveViewSettings() {
		StateHolder stateHolder = getStateHolder();
		if (stateHolder == null) {
			stateHolder = new StateHolder(getClass().getName(), getArguments());
			setStateHolder(stateHolder);
		}
		return stateHolder;
	}

	public void showNextScreen(BaseFragment fragment) {
		((MainActivity) getActivity()).showNextScreen(fragment);
	}

	public void freeFromLastParent() {
		if (mView != null) {
			ViewGroup v = ((ViewGroup) mView.getParent());
			if (v != null) {
				v.removeAllViews();
			}
		}

		// commented as it is making the fragment lose its properties
		// setStateHolder(null);
	}

	/**
	 * Should return true if the fragment has to animate in trasition while
	 * appearing or exiting
	 * 
	 * @return
	 */
	public boolean shouldAnimate() {
		return false;
	}

	/**
	 * keeps updating the state of the view like scrolling position, button
	 * clicked etc
	 * 
	 * @return
	 */
	public StateHolder getStateHolder() {
		return stateHolder;
	}

	/*
	 * Method to set the StateHolder
	 */
	public void setStateHolder(StateHolder stateHolder) {
		this.stateHolder = stateHolder;
	}

	/*
	 * Method to get the context in the fragment
	 * 
	 * @return Context
	 */
	public Context getFragmentContext() {
		return fragmentContext;
	}

	/*
	 * Method to set the Context value which will be accessed in the Fragment
	 */
	public void setFragmentContext(Context fragmentContext) {
		this.fragmentContext = fragmentContext;
	}

	/*
	 * Method called to check if the onBackPressed has been overridden in the
	 * fragments
	 */
	public boolean onBackPressed() {
		return false;
	}

	public void resetToReload() {
		mView = null;
		getStateHolder().setTag(null);
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	}

	/**
	 * @param keyCode
	 * @param event
	 * @return
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return false;
	}


	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 * @since Jan 24, 2014
	 * @author BalaKJ 
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}


}
